package com.ttl.bc.ap.stkrequest;


import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.stkrequest.BCStkRequestBean;
import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmBO;
import com.ttl.bc.ic.finishgoods.db.BCFinishGoodsBO;
import com.ttl.bc.ic.item.BCItemBO;
import com.ttl.common.util.CommonUtils;


public class BCStkRequestAction extends DispatchAction {
	
	public ActionForward blank(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");		
		//String issueType = (String)request.getParameter("issueType");
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");				
		
		//String issueTypeName = CommonUtils.getIssueType(Integer.valueOf(issueType));				
		String docNo 		 = CommonUtils.getMaxStkRequestDocNo(preDocNo);
		
//		request.getSession().setAttribute("userName", user);
//		request.getSession().setAttribute("issueType", issueType);
//		request.getSession().setAttribute("issueTypeName", issueTypeName);
		request.setAttribute("docNo", docNo);		
		request.setAttribute("docDate", CommonUtils.getDateForStamp());
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :blank");
		
		
		
		return mapping.findForward("blank");
	}
	
	public ActionForward approveShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		BCReqConfirmBO bo = new BCReqConfirmBO();
		BCStkRequestForm issueform = bo.approveShow(request, response);
				
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveShow "+
									  "#docNo:"+issueform.getDocNo());						
		
		//ต้องใช้ เลขที่เบิกกับขอเบิกอันเดียวกัน เพราะค้นหา เอกสารด้วย hibernate ยาก เพราะมันเป็บเก็บแบบ Set
		String stkIssueDocNo = issueform.getDocNo();//CommonUtils.getMaxStkIssueDocNo(issueform.getDocNo());
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("stkIssueDocNo", stkIssueDocNo);
		request.getSession().setAttribute("userName", user);
		
		return mapping.findForward("approveShow");			
	}	
	
	public ActionForward approveConfirm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
								
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveConfirm "+
									  "#docNo:"+request.getParameter("docNo"));												
		request.setAttribute("inPage", inPage);
		
		BCReqConfirmBO bo = new BCReqConfirmBO();
		
		if (bo.approveConfirm(request, response)==true) {
			request.setAttribute("message", "บันทึกรับของเรียบร้อย");
			return mapping.findForward("success_approve");
		}else{
			return mapping.findForward("fail_approve");
		}
	}	
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :add");
		
		BCStkRequestBO bo = new BCStkRequestBO();						
		if (bo.add(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			return mapping.findForward("success_add");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_add");
		}
	}

	public ActionForward addToFinishGoods_TTL2(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response) 
					throws Exception {
				
												
				String inPage = (String)request.getParameter("d-49653-p");
				String user   = (String)request.getSession().getAttribute("userName");
				
				System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :addToFinishGoods_TTL2 "+
											  "#docNo:"+request.getParameter("docNo"));		
												
				request.setAttribute("inPage", inPage);		
				
				BCFinishGoodsBO bo2 = new BCFinishGoodsBO();
				if (bo2.addToFinishGoods_TTL2(request, response)==true) {
					return mapping.findForward("success_addToFinishGoods_TTL2");
				}else{
					return mapping.findForward("fail_addToFinishGoods_TTL2");
				}				
				
				
			}
	
	
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :showFind");
		
		BCStkRequestBO bo = new BCStkRequestBO();
//		BCReqConfirmBO bo = new BCReqConfirmBO();
//		ArrayList results = (ArrayList)bo.find(request);		
//		request.getSession().setAttribute("results", results);						
		
		bo.showFind(request);
//		Page.setPage(request, "")
				
		return mapping.findForward("success_showFind2");
	}

	@SuppressWarnings("unchecked")
	public ActionForward showFindAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :showFind");
		
//		BCStkRequestBO bo = new BCStkRequestBO();
		BCReqConfirmBO bo = new BCReqConfirmBO();
		ArrayList results = (ArrayList)bo.findAll(request);
		
		request.getSession().setAttribute("results", results);
		
		return mapping.findForward("success_showFindAll");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :delete ");		
						
		BCStkRequestBO bo = new BCStkRequestBO();
														
		if (bo.delete(form, request, response)==true) {
			request.setAttribute("message", "ลบเอกสารเรียบร้อยแล้ว");
			return mapping.findForward("success_message");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete");
		}						
	}
	
	public ActionForward restore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :delete ");		
						
		BCStkRequestBO bo = new BCStkRequestBO();
														
		if (bo.restore(form, request, response)==true) {
			request.setAttribute("message", "เรียกเอกสารคืน เรียบร้อยแล้ว");
			return mapping.findForward("success_message");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_restore");
		}						
	}	

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :cancel "+
									  "#docNo:"+issue2form.getDocNo());		
		
		request.setAttribute("issue2form", issue2form);
		request.setAttribute("inPage", inPage);		
		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("success_cancel");		
	}
	
	public ActionForward cancelOfDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :cancelOfDelete "+
									  "#docNo:"+issue2form.getDocNo());		
		
		request.setAttribute("issue2form", issue2form);
		request.setAttribute("inPage", inPage);		
		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("success_cancelOfDelete");		
	}	
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :edit ");
			
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_edit");
	}
	
	public ActionForward beforeApprove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :beforeApprove ");
			
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_beforeApprove");
	}	

	public ActionForward receiveConfirmShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
//		String user 	 = (String)request.getSession().getAttribute("userName");
//		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :receiveConfirmShow ");
//			
//		BCStkRequestBO bo = new BCStkRequestBO();
//		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
//		
//		request.setAttribute("issue2form", issue2form);
//						
//		return mapping.findForward("blank_receiveConfirmShow");
//		BCStkIssueBO bo = new BCStkIssueBO();
//		BCStkIssue2Form issueform = bo.approveShow(request, response);
		BCReqConfirmBO bo = new BCReqConfirmBO();
		BCStkRequestForm issueform = bo.approveShow(request, response);		
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :receiveConfirmShow "+
									  "#docNo:"+issueform.getDocNo());		
		
		String preDocNoFG = "TI-TT-";
		
		String fgDocNo = CommonUtils.getMaxFinishGoodsDocNo(preDocNoFG);
		
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("fgDocNo", fgDocNo);
//		request.getSession().setAttribute("userName", user);
		
		return mapping.findForward("receiveConfirmShow");
		
	}	
	
	
	public ActionForward price(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :price ");
			
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_price");
	}
	
	public ActionForward updatePrice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :updatePrice");		
		
		BCStkRequestBO bo = new BCStkRequestBO();
		
		if (bo.updatePrice(form, request, response)== true ) {			
			request.setAttribute("message", "บันทึกข้อมูลราคาต่อหน่วยเรียบร้อย");
			return mapping.findForward("success_message");	
		}else {
			return mapping.findForward("fail_update");
		}												
	}	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :update");		
		
		BCStkRequestBO bo = new BCStkRequestBO();
		
		if (bo.update(form, request, response)== true ) {			
			request.setAttribute("message", "บันทึกการแก้ไขข้อมูลเรียบร้อย");
			return mapping.findForward("success_message");	
		}else {
			return mapping.findForward("fail_update");
		}												
	}
	public ActionForward updateForApprove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :updateForApprove");		
		
		BCStkRequestBO bo = new BCStkRequestBO();
		
		if (bo.updateForApprove(form, request, response)== true ) {			
			request.setAttribute("message", "บันทึกการแก้ไขข้อมูลเรียบร้อย");
			return mapping.findForward("success_message");	
		}else {
			return mapping.findForward("fail_update");
		}												
	}
	public ActionForward moreItemsShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :moreItemsShow ");
				
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ""+((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8").trim();
			request.setAttribute("itemFind", itemFind);
		}
		
//		String hql = "FROM BCItemBean WHERE code like '%"+itemFind+"%' or name1 like '%"+itemFind+"%'  ORDER BY code";
		String hql = "FROM View_BCStkLocation WHERE (itemCode like '%"+itemFind+"%' or name1 like '%"+itemFind+"%') " +
				" and whCode='10' and shelfCode='10'  ORDER BY itemCode";
		
		new BCItemBO().setPage(request, hql);
								
		return mapping.findForward("moreItemsShow");
	}	
	
	public ActionForward departmentShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :departmentShow ");
		request.setAttribute("lineNumber", request.getParameter("lineNumber"));						
		return mapping.findForward("departmentShow");
	}

	public ActionForward projectShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :projectShow ");
								
		return mapping.findForward("projectShow");
	}	

	public ActionForward unitCodeShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :unitCodeShow ");
		request.setAttribute("lineNumber", request.getParameter("lineNumber"));							
		return mapping.findForward("unitCodeShow");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward print(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String docNoParam = (String)request.getParameter("docNo");

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestAction #mode :print ");		
		System.out.println("#docNo = "+docNoParam);
		
		String hql = "from BCStkRequestBean where docNo = '"+docNoParam+"'";
		BCStkRequestBO bo = new BCStkRequestBO();
		ArrayList results = (ArrayList)bo.findPrint(hql);

		String nameOfReport = "";
		
		if (results != null ) {
			BCStkRequestBean s2 = (BCStkRequestBean)results.get(0);			
			
			if (s2.getBcStkRequestSubs().size()==1) {
				//System.out.println("=1");
				nameOfReport = "/ireport/rptBuyRequest_1lineItem.jasper";
			}else {
				//System.out.println(">1");
				nameOfReport = "/ireport/rptBuyRequest.jasper";
			}
		}
					
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
		
		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
		
		return null;			
	}
}
